﻿
window.getURLParam=function(name){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
};

Array.prototype.forEach=function(f){
	var l=this.length;
	for(var i=0;i<l;i++) {
		if(f(this[i],i,this)==false){
			break;
		}
	}
};

RegExp.escape = function(text) {
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};

String.prototype.escapeHTML=function() {
    return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
};

Date.prototype.format=function(format){
	if(format!=null&&typeof(format)=="string"){
		var dt;
		return format.replace(/yyyy/g,this.getFullYear())
			.replace(/MM/g,(dt=this.getMonth()+1)<10?"0"+dt:dt)
			.replace(/dd/g,(dt=this.getDate())<10?"0"+dt:dt)
			.replace(/hh/g,(dt=this.getHours())<10?"0"+dt:dt)
			.replace(/mm/g,(dt=this.getMinutes())<10?"0"+dt:dt)
			.replace(/ss/g,(dt=this.getSeconds())<10?"0"+dt:dt);
	} else {
		return this.format("yyyy-MM-dd hh:mm:ss");
	}
};

Date.parsef=function(str,format){
	if(format==null){
		format="yyyy-MM-dd hh:mm:ss";
	}

	var supported=["yyyy","MM","dd","hh","mm","ss"];

	var ts=[];
	var formatLen=format.length;
	for(var i=0;i<formatLen;i++){
		ts.push(null);
	}

	var regFormat=format;
	supported.forEach(function(e,i,arr){
		var fi=format.search(e);
		if(fi>-1){
			if(e=="yyyy"){
				regFormat=regFormat.replace(e,"(\\d\\d\\d\\d)");
			}else{
				regFormat=regFormat.replace(e,"(\\d{1,2})");
			}
			ts[fi]=e;
		}
	});

	var idxs=[];
	var counter=1;
	idxs.length=supported.length;
	ts.forEach(function(e,i,arr){
		if(e!=null){
			var paramIdx=-1;
			supported.forEach(function(se,si,sarr){
				if(se==e){
					paramIdx=si;
					return false;
				}
			});
			idxs[paramIdx]=counter++;
		}
	});
	var vals=new RegExp(regFormat).exec(str);
	
	return new Date(parseInt(vals[idxs[0]],10),
					parseInt(vals[idxs[1]],10)-1,
					parseInt(vals[idxs[2]],10),
					idxs[3]==null?0:parseInt(vals[idxs[3]],10),
					idxs[4]==null?0:parseInt(vals[idxs[4]],10),
					idxs[5]==null?0:parseInt(vals[idxs[5]],10));
};